package com.yalu.service.data.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yalu.dao.data.NetworkioReportDao;
import com.yalu.service.data.NetworkioReportService;
import com.yalu.util.ReportExportUtil;
import com.yalu.util.ReportHeaderUtil;

import core.report.yearReport.bean.BaseExcelReportDataBean;
import core.report.yearReport.bean.MergedBean;
import core.report.yearReport.bean.Networkio;
import core.report.yearReport.bean.SalesReportDataBean;
import core.report.yearReport.util.ExportExcelUtil;

@Service
public class NetworkioReportServiceImpl implements NetworkioReportService {

	@Resource
	private NetworkioReportDao networkioReportDao;

	@Override
	public void createReport(String dateType, String beginTime, String endTime,
			String resultFileName) {
		System.out.println("------------1");
		List<Networkio> dataList = networkioReportDao.getNetworkioReportDataBeanList(
				dateType, beginTime, endTime);
		System.out.println("------------2");
		// 平台总计
		List<BaseExcelReportDataBean> excelDataList = returnExcelDataList(
				dateType, dataList);
		System.out.println("------------3");
		List<MergedBean> headers = ReportHeaderUtil.getHeaders(dateType,
				beginTime, endTime, null);
		ExportExcelUtil ex = new ExportExcelUtil();
		OutputStream out;
		try {
			File file = new File(resultFileName);
			out = new FileOutputStream(file);
			ex.exportExcel(ReportHeaderUtil.getSheetNameByDateType(dateType),
					headers, excelDataList, out, ReportExportUtil
							.getBeginRowIndexByDateType(dateType),
					ReportHeaderUtil.getColCountByDateType(dateType), null);
			System.out.println("------------4");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private List<BaseExcelReportDataBean> returnExcelDataList(String dateType,
			List<Networkio> dataList) {
		List<BaseExcelReportDataBean> excelDataList = new ArrayList<BaseExcelReportDataBean>();
		Networkio propertiesBean = new Networkio();// 平台总计
		propertiesBean.setAttributeFlag(true);
		Networkio totalBean = new Networkio();// 全部店铺总计
		totalBean.setAttributeFlag(true);
		totalBean.setName("合计");
		Networkio lastData = null;
		int addCount = 0;
		for (int index = 0; index < dataList.size(); index++) {
			Networkio data = dataList.get(index);
			totalBean.addProperties(data);
			if (lastData == null) {
				propertiesBean.setAttribute_id(data.getAttribute_id());
				propertiesBean.setName(ReportExportUtil
						.getAttributeNameById(data.getAttribute_id()));
				propertiesBean.addProperties(data);
				excelDataList.add(data);
			} else {
				if (lastData.getAttribute_id() == data.getAttribute_id()) {
					propertiesBean.addProperties(data);
					excelDataList.add(data);
				} else {
					propertiesBean.subtract(addCount);
					excelDataList.add(propertiesBean);
					addCount = 0;
					excelDataList.add(data);
					propertiesBean = new Networkio();
					propertiesBean.setAttributeFlag(true);
					propertiesBean.setAttribute_id(data.getAttribute_id());
					propertiesBean.setName(ReportExportUtil
							.getAttributeNameById(data.getAttribute_id()));
					propertiesBean.addProperties(data);
				}
			}
			if (index == dataList.size() - 1) {
				excelDataList.add(propertiesBean);
			}
			lastData = data;
			addCount++;
		}
		if (dataList.size() != 0) {
			totalBean.subtract(dataList.size());
			excelDataList.add(totalBean);
		}
		return excelDataList;
	}
}
